package com.one2manycall.conf;

import lombok.extern.slf4j.Slf4j;
import com.one2manycall.service.websocket.CallHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * 配置媒体服务
 */
@Configuration
@Slf4j
public class WebSocketConfig implements WebSocketConfigurer {


    public static final String webSocket_address_pre = "/base";

    /**
     * 注册 消息处理类
     *
     * @return CallHandler
     */
    @Bean
    public CallHandler callHandler() {
        return new CallHandler();
    }


    @Bean
    public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(32768);
        return container;
    }


    /**
     * websocket 监听地址配置 和web浏览器页面通讯的地址 /call 开头声明
     *
     * @param registry registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

        registry.addHandler(callHandler(), webSocket_address_pre).setAllowedOrigins("*");


        log.info("registry webSocket address :" + webSocket_address_pre);
    }


}
